package com.atguigu.guli.service.ucenter.controller.api;


import com.alibaba.csp.sentinel.util.StringUtil;
import com.atguigu.guli.common.util.FormUtils;
import com.atguigu.guli.service.base.dto.MemberDto;
import com.atguigu.guli.service.base.helper.JwtHelper;
import com.atguigu.guli.service.base.helper.JwtInfo;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.ucenter.entity.form.LoginForm;
import com.atguigu.guli.service.ucenter.entity.form.RegisterForm;
import com.atguigu.guli.service.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2020-11-02
 */
@RestController

@Api(tags = "会员管理")
@RequestMapping("/api/ucenter/member")
public class ApiMemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private RedisTemplate redisTemplate;



    @ApiOperation("根据Id查询会员信息")
    @GetMapping("get-member-dto/{memberId}")
    public R getMemberDtoById(@PathVariable String memberId){
        MemberDto memberDto = memberService.getMemberDtoById(memberId);
        return R.ok().data("memberDto",memberDto);
    }


    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("/auth/get-login-info")
    public R getLoginInfo(HttpServletRequest request){

        JwtInfo jwtInfo = JwtHelper.getJwtInfo(request);
        return R.ok().data("userInfo", jwtInfo);
    }


    @ApiOperation("会员登陆")
    @PostMapping("/login")
    public R login(@RequestBody LoginForm loginForm){
        String mobile = loginForm.getMobile();
        String password = loginForm.getPassword();
        boolean isMobile = FormUtils.isMobile(mobile);
        if (isMobile){
            if (!StringUtil.isEmpty(password)){
                String token = memberService.login(loginForm);
                return R.ok().data("token",token).message("登陆成功");
            }else {
                return R.setResult(ResultCodeEnum.LOGIN_PASSWORD_ERROR);
            }
        }else {
            return R.setResult(ResultCodeEnum.LOGIN_PHONE_ERROR);
        }
    }



    @PostMapping("/register")
    @ApiOperation("会员注册")
    public R register(@RequestBody RegisterForm registerForm){
        String nickname = registerForm.getNickname();
        String mobile = registerForm.getMobile();
        String code = registerForm.getCode();
        String password = registerForm.getPassword();

        ///校验参数
        if (StringUtils.isEmpty(mobile)
                || !FormUtils.isMobile(mobile)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code)
                || StringUtils.isEmpty(nickname)) {
            return R.setResult(ResultCodeEnum.PARAM_ERROR);
        }

        //检验验证码
        String key = "checkCode::" + mobile;
        String checkCode = (String)redisTemplate.opsForValue().get(key);
        if(!code.equals(checkCode)){
            return R.setResult(ResultCodeEnum.CODE_ERROR);
        }

        memberService.register(registerForm);
        return R.ok().message("注册成功");
    }

}

